package com.yanfan;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yanfan.access.domain.NjAccessChannelInfo;
import com.yanfan.access.domain.NjAccessDeviceInfo;
import com.yanfan.access.domain.NjAccessRecords;
import com.yanfan.access.domain.NjPersonInfo;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.ibms.common.utils.HttpUtils2;
import com.yanfan.utils.OauthUtil;
import com.yanfan.utils.RSAUtil;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Access {
    /**
     * 认证
     * @param args
     */
    public static void main(String[] args) throws Exception {
       /* *
         * 获取门禁设备列表
*/
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        int[] categorys = {8};
        String[] types = {"8_16"};
        param.put("pageNum",1);
        param.put("pageSize",999);
        param.put("categorys",categorys);
        //param.put("types",types);
        JSONObject jsonObject1 = HttpUtils2.
                sendPostSSLBody("https://192.168.254.6:443/evo-apigw/evo-brm/1.2.0/device/subsystem/page"
                        ,headers,param);
        //System.out.println(jsonObject1.toString());
        JSONObject jsonObject = JSONObject.parseObject(jsonObject1.toString());
        JSONObject data = JSONObject.parseObject(jsonObject.get("data").toString());
        JSONArray pageData = JSON.parseArray(data.get("pageData").toString());
        ArrayList<NjAccessDeviceInfo> deviceList = new ArrayList<>();
        for (Object pageDatum : pageData) {
            JSONObject jsonObject3 = JSONObject.parseObject(pageDatum.toString());
            JSONObject jsonObject2 = HttpUtils2.sendSLLGetParm(
                    "https://192.168.254.6:443/evo-apigw/evo-brm/1.0.0/device/" + jsonObject3.get("deviceCode"),
                    headers, null);
            //拿到设备数据
            String data1 = jsonObject2.get("data").toString();
            NjAccessDeviceInfo deviceInfo = JSONObject.parseObject(data1, NjAccessDeviceInfo.class);
            deviceList.add(deviceInfo);
            System.out.println(deviceInfo);
        }
        //获取通道信息
        ArrayList<NjAccessChannelInfo> channelInfoList = new ArrayList<>();
        for (Object pageDatum : pageData) {
            JSONArray units = JSON.parseArray(JSONObject.
                    parseObject(pageDatum.toString()).getString("units"));
            for (Object un : units) {
                JSONObject unitJson = JSONObject.parseObject(un.toString());
                JSONArray channels = JSON.parseArray(unitJson.get("channels").toString());
                List<NjAccessChannelInfo> channelList = JSON.
                        parseArray(channels.toString(), NjAccessChannelInfo.class);
                channelList.stream().forEach(channel->{
                    channelInfoList.add(channel);
                });
            }
        }
        System.out.println("通道列表：" + channelInfoList);
        System.out.println(pageData.get(0));
        System.out.println(pageData.get(1));
        System.out.println(pageData.get(2));
        //1:MTKqbPyEKXK0jzAv1I234cgbYA4LzjCP
    }

    /**
     * 获取人员列表
     */
    @Test
    public void getPersonList() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        int[] categorys = {8};
        String[] statusList = {"0","1","2"};
        param.put("pageNum",1);
        param.put("pageSize",999);
        param.put("statusList",statusList);
        //param.put("types",types);
        JSONObject jsonObject1 = HttpUtils2.
                sendPostSSLBody("https://192.168.254.6:443" +
                                "/evo-apigw/evo-brm/1.2.0/person/subsystem/page"
                        ,headers,param);
        //System.out.println(jsonObject1.toString());
        JSONObject jsonObject = JSONObject.parseObject(jsonObject1.toString());
        JSONObject data = JSONObject.parseObject(jsonObject.get("data").toString());
        List<NjPersonInfo> pageData = JSON.parseArray(data.get("pageData").toString(), NjPersonInfo.class);
        pageData.stream().forEach(person->{
            if (person.getPersonBiosignatures() != null){
                JSONArray jsonArray = person.getPersonBiosignatures();
                Object path = JSONObject.parseObject(jsonArray.get(0).toString()).get("path");
                person.setImageUrl(path.toString());
            }
        });
        System.out.println(pageData);
    }

    /*public static void main(String[] args) throws Exception {
        *//* *
         * 获取门禁设备状态
         *//*
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
       *//* int[] categorys = {8};
        String[] types = {"8_16"};
        param.put("categorys",categorys);
        param.put("types",types);*//*
        param.put("channelCode","1000141$7$0$0");
        JSONObject jsonObject1 = HttpUtils2.
                sendPostSSLBody("https://192.168.254.6:443" +
                                "/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/channelControl/channels"
                        ,headers,param);
        //System.out.println(jsonObject1.toString());
        JSONObject jsonObject = JSONObject.parseObject(jsonObject1.toString());
        JSONObject data = JSONObject.parseObject(jsonObject.get("data").toString());
        JSONArray pageData = JSON.parseArray(data.get("pageData").toString());
        ArrayList<NjAccessDeviceInfo> deviceList = new ArrayList<>();
        for (Object pageDatum : pageData) {
            JSONObject jsonObject3 = JSONObject.parseObject(pageDatum.toString());
            JSONObject jsonObject2 = HttpUtils2.sendSLLGetParm(
                    "https://192.168.254.6:443/evo-apigw/evo-brm/1.0.0/device/" + jsonObject3.get("deviceCode"),
                    headers, param);
            //拿到设备数据
            String data1 = jsonObject2.get("data").toString();
            NjAccessDeviceInfo deviceInfo = JSONObject.parseObject(data1, NjAccessDeviceInfo.class);
            deviceList.add(deviceInfo);
            System.out.println(deviceInfo);
        }

       *//* System.out.println(pageData.get(0));
        System.out.println(pageData.get(1));
        System.out.println(pageData.get(2));*//*
        //1:MTKqbPyEKXK0jzAv1I234cgbYA4LzjCP
    }*/

    /**
     * 门禁记录
     * @param args
     * @throws Exception
     */
   /* public static void main(String[] args) throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",999);
        // 获取当前时间字符串格式
        String time = DateUtils.getTime();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串解析为 LocalDateTime 对象
        LocalDateTime dateTime = LocalDateTime.parse(time, formatter);
        // 减少10分钟
        LocalDateTime newTime = dateTime.minusMinutes(10);
        // 转换为字符串格式
        String startTime = newTime.format(formatter);
        param.put("startSwingTime",startTime);
        param.put("endSwingTime", time);
        JSONObject jsonObject = HttpUtils2.
                sendPostSSLBody(
                        "https://192.168.254.6:443/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/swingCardRecord/bycondition/combined"
                        ,headers,param);
        JSONObject data = JSONObject.parseObject(jsonObject.get("data").toString());
        List<NjAccessRecords> pageData = JSON.parseArray(data.get("pageData").toString(), NjAccessRecords.class);

        System.out.println(pageData.get(1));
        System.out.println(pageData.get(2));
        System.out.println(pageData.get(3));
    }*/

    /*public static void main(String[] args) throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",999);
        JSONObject jsonObject1 = HttpUtils2.
                sendSLLGetParm(
                        "https://192.168.254.6:443/evo-apigw/ipms/parkSurvey/platformDetail"
                        ,headers,null);
        System.out.println(jsonObject1.toString());
    }*/

    /**
     * 车场信息
     * @param args
     * @throws Exception
     */
   /* public static void main(String[] args) throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",999);
        JSONObject jsonObject1 = HttpUtils2.
                sendSLLGetParm(
                        "https://192.168.254.6:443/evo-apigw/ipms/parkinglot/query"
                        ,headers,param);
        System.out.println(jsonObject1.toString());
    }*/

    /**
     * 停车记录
     * @param args
     * @throws Exception
     */
    /*public static void main(String[] args) throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",999);
        JSONObject jsonObject1 = HttpUtils2.
                sendSLLGetParm(
                        "https://192.168.254.6:443/evo-apigw/ipms/parkinglot/query"
                        ,headers,param);
        System.out.println(jsonObject1.toString());
    }*/

    /**
     * 报警主机状态
     * @param args
     * @throws Exception
     */
    /*public static void main(String[] args) throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",999);
        JSONObject jsonObject1 = HttpUtils2.
                sendPostSSLBody(
                        "https://192.168.254.6:443/evo-apigw/evo-alarm/1.0.0/alarmhosts/all"
                        ,headers,param);
        System.out.println(jsonObject1.toString());
    }*/

    /**
     * 报警主机布防
     * @param args
     * @throws Exception
     */
    /*public static void main(String[] args) throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",999);
        JSONObject jsonObject1 = HttpUtils2.
                sendSLLGetParm(
                        "https://192.168.254.6:443/evo-apigw/evo-alarm/1.0.0/alarmhosts/one/1000264"
                        ,headers,null);
        System.out.println(jsonObject1.toString());
    }*/

    @Test
    public void getFanke() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",999);
        JSONObject jsonObject = HttpUtils2.
                sendPostSSLBody(
                        "https://192.168.254.6:443" +
                                "/evo-apigw/evo-visitor/1.0.0/card/accessControl/swingCardRecord/visitor/bycondition/combined"
                        ,headers,param);
        System.out.println(jsonObject.toString());
    }

    /**
     * 人脸库
     * @throws Exception
     */
    @Test
    public void getFaceGroupInfo() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum","1");
        param.put("pageSize",9999);
        param.put("searchType","2");
        JSONObject jsonObject = HttpUtils2.
                sendSLLGetParm(
                          "https://192.168.254.6:443/evo-apigw/evo-face/groupInfo/page?pageNum=1&pageSize=999&searchType=2"
                        ,headers,null);
        JSONArray jsonArray = JSON.parseArray(JSONObject.parseObject(jsonObject.
                get("data").toString()).get("pageData").toString());
    }


}
